﻿using System;
using System.Net;
using System.Text;

namespace UrlCaller
{
    public class UrlCall
    {
        int threadno;
        string url;
        StringBuilder stringBuilder;
        public UrlCall(int threadno, StringBuilder stringBuilder, string url)
        {
            this.url = url;
            this.threadno = threadno;
            this.stringBuilder = stringBuilder;
        }
 
        public void Execute()
        {
            var start = DateTime.Now;
            
            var webRequest = WebRequest.Create(url);
            
            try
            {
                using (WebResponse response = webRequest.GetResponse())
                {

                }

                    stringBuilder.AppendLine
                        (
                            string.Format
                            (
                                "Success in thread {0}. Took {1} seconds...",
                                threadno,
                                (DateTime.Now - start).Seconds
                            )
                       );
            }
            catch (Exception ex)
            {

                stringBuilder.AppendLine
                (
                    string.Format
                    (
                        "Error in thread {0}. Took {1} seconds. Error{2}",
                        threadno,
                        (DateTime.Now - start).Seconds,
                        ex.Message
                    )
               );

            }


        }
    }
}
